//寻找数组的中心下标
//测试链接 https://leetcode.cn/problems/find-pivot-index/
public class PivotIndex {
    public int pivotIndex(int[] nums) {
        int n = nums.length;

        long[] dpLeft = new long[n];  //从左向右计算和
        long[] dpright = new long[n]; //从右向左计算和

        long sumleft = 0;
        long sumright = 0;
        for(int i = 0, j = n-1; i < n; i++, j--){
            sumleft += nums[i];
            sumright += nums[j];

            dpLeft[i] = sumleft;
            dpright[j] = sumright;
        }

        for(int i = 0; i < n; i++){
            if(dpLeft[i] == dpright[i]){
                return i;
            }
        }

        return -1;
    }
}
